package com.yoo.module_base.base

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.yoo.module_base.R
//BaseDialogFragment基类
open class BaseDialogFragment : DialogFragment() {
    private val classSimpleName by lazy { javaClass.simpleName }
    //private val logger by lazy { Logger(javaClass.simpleName) }
    open fun hasShadow() = false

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return Dialog(
            requireActivity(),
            if (hasShadow()) R.style.dialog2 else R.style.dialog_dim_false
        ).apply {
            setCanceledOnTouchOutside(true)
            window?.let(::setWindow)
        }
    }

    fun show(fragmentManager: FragmentManager?) {
        when {
            fragmentManager == null -> Log.i(
                classSimpleName,
                "fragmentManager is detach after parent destroy"
            )

            fragmentManager.isStateSaved -> Log.i(
                classSimpleName,
                "dialog fragment show when fragmentManager isStateSaved"
            )

            else -> show(fragmentManager, classSimpleName)
        }
    }

    open fun setWindow(win: Window) {
        win.setWindowAnimations(R.style.BaseDialog_AnimationStyle)
        win.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        win.attributes = lp
        win.setGravity(Gravity.BOTTOM)
    }

}